﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;

using SceneEngine;

namespace SceneEditor
{
    public partial class Editor
    {
        void btnCameras_ButtonClick(object sender, EventArgs e)
        {
            if (editor.tsMaya.Checked)
                tsFirstPerson_Click(placingObjects, e);
            else if (editor.tsFirstPerson.Checked)
                tsMaya_Click(placingObjects, e);
        }

        void tsMaya_Click(object sender, EventArgs e)
        {
            if (editor.tsMaya.Checked)
                return;

            editor.tsMaya.Checked = true;
            editor.tsFirstPerson.Checked = false;

            float yaw = (camera as Camera).Yaw;
            float pitch = (camera as Camera).Pitch;

            camera = new MayaStyleCamera(camera.Position, camera.Forward, camera.Speed);

            (camera as MayaStyleCamera).HorizontalAngle = yaw;
            (camera as MayaStyleCamera).Yaw = -pitch;

            EngineGlobals.Camera = camera;

            Preferences.Default.MouseControls = true;
            Preferences.Default.Save();
        }
        void tsFirstPerson_Click(object sender, EventArgs e)
        {
            if (editor.tsFirstPerson.Checked)
                return;

            editor.tsFirstPerson.Checked = true;
            editor.tsMaya.Checked = false;

            Vector3 forward = camera.Forward;

            camera = new Camera(camera.Position, camera.Speed);
            camera.ChangeDirection(forward);

            EngineGlobals.Camera = camera;

            Preferences.Default.MouseControls = false;
            Preferences.Default.Save();
        }

        private void btnCenterCamera_Click(object sender, EventArgs e)
        {
            if (gizmo.Transforms.Count <= 0)
                return;

            BoundingSphere nodeSphere = BoundingSphere.CreateFromBoundingBox(gizmo.Transforms.Bounds);

            camera.Position = nodeSphere.Center;

            float distanceToCenter = nodeSphere.Radius / (float)Math.Sin(MathHelper.PiOver4 / 2);

            Vector3 back = -camera.Forward;

            camera.Position += (back * distanceToCenter);

            if (camera is MayaStyleCamera)
                (camera as MayaStyleCamera).Zoom = distanceToCenter;

            Vector3 direction = nodeSphere.Center - camera.Position;
            direction.Normalize();

            if (camera is Camera)
                camera.ChangeDirection(direction);
            else if (camera is MayaStyleCamera)
                camera.ChangeDirection(nodeSphere.Center);

            editor.Viewport.Select();
        }
    }
}
